﻿using System.Runtime.Serialization;
using System.Windows.Media;

using Game.ResourceLocator;

namespace Game.Terrain
{
    [DataContract(Namespace="game")]
    public class TerrainType : IHasUrl
    {
        public static TerrainType DefaultTerrainType { get; set; }

        ReadOnlyTerrainBits _TerrainBits;
        ImageSource _ImageSource; 
        string _Name;
        int _AttackBonus;
        int _DefenseBonus;
        
        [DataMember(Name="Url")]
        public readonly string _Url;
        
        public TerrainType()
        {
        }
        
        [OnDeserialized]
        public void OnDeserializing(StreamingContext c)
        {
            object obj = ResourceLocator.GameResourceLocator.Get(_Url);
            TerrainType t = obj as TerrainType;
            if (t != null)
            {
                _ImageSource = t.ImageSource;
                _TerrainBits = t.TerrainBits;
                _Name = t.Name;
                _AttackBonus = t.AttackBonus;
                _DefenseBonus = t.DefenseBonus;
            }
        }
        
        public TerrainType(TerrainTypeBuilder builder)
        {
            _ImageSource = builder.ImageSource;
            _TerrainBits = builder.TerrainBits.ToReadOnly();
            _Name = builder.Name;
            _Url = builder.Url;
            _AttackBonus = builder.AttackBonus;
            _DefenseBonus = builder.DefenseBonus;
        }
        
        public string Url
        {
            get { return _Url; }
        }
        
        public string Name
        {
            get { return _Name; }
        }
        
        public ImageSource ImageSource
        {
            get 
            { 
                return _ImageSource; 
            }
        }
        
        public int AttackBonus
        {
            get { return _AttackBonus; }
        }
            
        public int DefenseBonus
        {
            get { return _DefenseBonus; }
        }
        
        public ReadOnlyTerrainBits TerrainBits
        {
            get { return _TerrainBits; }
        }
    }
    
    public class TerrainTypeBuilder
    {
        string _Url;
            
        public TerrainTypeBuilder()
        {
            TerrainBits  = new TerrainBits();
        }
        
        public ImageSource ImageSource { get; set; }
        
        public TerrainBits TerrainBits { get; set; }
        
        public string Name { get; set; }
        
        public int AttackBonus { get; set; }
        
        public int DefenseBonus { get; set; }
        
        public string Url
        {
            get 
            {
                if (_Url == null)
                {
                    return "terrain:" + Name;
                }
                
                return _Url; 
            }
            set { _Url=value; }
        }
    }
    
}
